package edu.hws.GA;

//package edu.hws.GA;

import java.awt.*;
import java.awt.event.*;

class RadioGroupMenu extends Menu implements ItemListener {

   private CheckboxMenuItem[] items;
   private int selectedIndex = -1;
   
   RadioGroupMenu(String menuName, String[] itemNames) {
      this(menuName, itemNames, -1);
   }

   RadioGroupMenu(String menuName, String[] itemNames, int selected) {
      super(menuName);
      items = new CheckboxMenuItem[itemNames.length];
      for (int i = 0; i < itemNames.length; i++) {
         items[i] = new CheckboxMenuItem(itemNames[i]);
         add(items[i]);
         items[i].addItemListener(this);
      }
      selectedIndex = selected;
      if (selectedIndex < 0 || selectedIndex >= items.length)
          selectedIndex = 1;
      items[selectedIndex].setState(true);
   }
   
   public int getSelectedIndex() {
      return selectedIndex;
   }
   
   public void itemStateChanged(ItemEvent evt) {  // This works on other systems
      CheckboxMenuItem newSelection = (CheckboxMenuItem)evt.getSource();
      for (int i = 0; i < items.length; i++) {
         if (newSelection == items[i]) {
            items[selectedIndex].setState(false);
            selectedIndex = i;
            newSelection.setState(true);
            return;
         }
      }
   }

}
